<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4
 * Time: 11:08
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Diseases extends Controller
{
    public $list=[];
    public $compare=0;
    //数据整理
    public function index(){
        $data=Db::name('diseases')->select();
        $z1=''; $z2=''; $z3=''; $z4=''; $z5=''; $z6='';
        foreach ($data as $w=>$a){
            if(empty($a['type'])){
                $data[$w]['type']=0;
                $data[$w]['pid']=0;
                $z1=$a['id'];
            }else if($a['type']=='—'){
                $data[$w]['type']=1;
                $z2=$a['id'];
                $data[$w]['pid']=$z1;
            }else if($a['type']=='——'){
                $data[$w]['type']=2;
                $z3=$a['id'];
                $data[$w]['pid']=$z2;
            }else if($a['type']=='———'){
                $data[$w]['type']=3;
                $z4=$a['id'];
                $data[$w]['pid']=$z3;
            }else if($a['type']=='————'){
                $data[$w]['type']=4;
                $z5=$a['id'];
                $data[$w]['pid']=$z4;
            }else if($a['type']=='—————'){
                $data[$w]['type']=5;
                $z6=$a['id'];
                $data[$w]['pid']=$z5;
            }else if($a['type']=='——————'){
                $data[$w]['type']=6;
                $data[$w]['pid']=$z6;
            }
        }
        Db::name('diseases_copy')->insertAll($data);
    }

    public function showMessage(){
        $table=Db::name('diseases_copy');
        $result=[]; //返回数据
        $list=[]; //句子整合
        if($this->request->isPost()){
            $data=$this->request->param('text');
            $res=$this->tableSe($data);
            if(empty($res)){
                $result=['data'=>'没有数据'];
            }else{
//                dump($res);
                //查询父级
                $res=$this->fdfather($res);
//                dump($res);
                //查询所有条件
                $res=$this->tableFd($res);
//                dump($res);
                //组装语句
                $res=$this->messageList($res);
                //组合最终查询语句
                $this->endlist($res);
                //比对 方案1
                $res=$this->compare($data);
                //比对 方案2
//                $res=$this->compareT($data);
            }

            return json_encode($res);
        }
        return $this->fetch();
    }

    public function fdfather($data){
        $list=[];
        foreach ($data as $a){
            if($a['pid']!=0){
                $id=$this->fdfatherT($a['pid']);
            }else{
                $id=$a['id'];
            }
            if(!empty($id)){
                $list[$id]=Db::name('diseases_copy')->where('id',$id)->find();
            }
        }
//dump($list);
        return $list;
    }
    public function fdfatherT($id){
        $res=Db::name('diseases_copy')->where('id',$id)->field('desc,id,pid,code')->find();
        if($res['pid']!=0){
            return $this->fdfatherT($res['pid']);
        }
        return $res['id'];
    }

    //判断开始id
    public function tableSe($data,$num=4){
        $table=Db::name('diseases_copy');
        $start=substr($data,0,$num*3);
        $where['desc']=['like','%'.$start.'%'];
        $res=$table->where($where)->select();
        if(empty($res)){
            if($num>1){
                $num=$num-1;
                return $this->tableSe($data,$num);
            }else{
                return false;
            }
        }else{
            return $res;
        }
    }
    //追寻下一级数据
    public function tableFd($data){
        if(empty($data)) return false;
        foreach ($data as $s=>$a){
            $res=Db::name('diseases_copy')->where('pid',$a['id'])->field('desc,id,pid,code')->select();
            if(empty($res)){
                continue;
            }else{
                $data[$s]['son']=$this->tableFd($res);
            }
        }
        return $data;
    }
    //整合数据
    public function messageList($data,$str=''){
        foreach ($data as $a=>$b){
            $data[$a]['desc']=$str.$b['desc'];

            if(isset($b['son'])){
                $data[$a]['son']=$this->messageList($b['son'],$str.$b['desc']);
            }
        }
        return $data;
    }
    //最终语句
    public function endlist($data){
        foreach ($data as $a=>$b){
            if (isset($b['son'])){
                $this->endlist($b['son']);
            }else{
                $this->list[]=$b;
            }
        }
    }
    //对比 方案1
    public function compare($data){
        $arr=$this->list;
        foreach ($arr as $a=>$b){
            //若全等 直接返回
//            if($b['desc']==$data){
//                $res[]=$b;
//                return $res;
//            }
            //过滤掉长度差距太大的
//            $len=(mb_strlen($b['desc']))-(mb_strlen($data));
            $compare=similar_text($b['desc'],$data);
//            if($len<-6 || $len>6) {
//               unset($arr[$a]);
//               continue;
//            }
            if($compare>$this->compare){
                $this->compare=$compare;
            }
            $arr[$a]['compare']=$compare;
        }
        usort($arr,$this->ss('compare'));
        //追加正则匹配
        $res_list=[];//最终返回结果
        $reg="/".$data."/";
        foreach ($arr as $v=>$a){
            $res=[];
            $rex[0] = "/\([\s\S]*\)/i";
            $rex[1] = "/[0-9]+-[0-9]+/";
            $rex[2] = "/[0-9]+×[0-9]+/";
            $str=preg_replace($rex,"",$a['desc']);
            preg_match($reg,$str,$res);
            if(empty($res)){
                unset($arr[$v]);
            }
        }
        return $arr;
    }
    public  function ss($key)
    {
        return function ($a, $b) use ($key) {
            return $a[$key] < $b[$key];     //通过改变大于、小于来正向反向排序
        };
    }

    //对比 方案2
    public function compareT($data){
        $arr=$this->list;
        $leng=mb_strlen($data)/3;
        foreach ($arr as $a=>$b){
//           if($b['desc']==$data){
//               return $b;
//           }
            $compare=0; //拥有相同的汉字数量
            $newleng=mb_strlen($b['desc'])/3;
            $len=$leng-$newleng;
//           dump($len);dump($data);dump($b['desc']);
//           echo '=====';
            if($len>=-2 and $len<=2){
                $arr[$a]['compare']=$this->compareRule($data,$b['desc']);
            }else{
                unset($arr[$a]);
            }
        }
        usort($arr,$this->ss('compare'));
        return $arr;
    }
    public function compareRule($data1,$data2){
        if(strstr($data1, '(')){
            $data1=preg_replace('(','',$data1);
        }
        if(strstr($data2, '(')){
            $data2=preg_replace('(','',$data2);
        }
        if(strstr($data1, ')')){
            $data1=preg_replace(')','',$data1);
        }
        if(strstr($data2, ')')){
            $data2=preg_replace(')','',$data2);
        }

        $len=mb_strlen($data1)/3;
        $compare=0;
        for ($i=1;$i<$len;$i++){
            $str=substr($data1,($i-1)*3,3);
            $reg="/$str+/";
            preg_match($reg,$data2,$res);
            if(!empty($res)){
                $compare++;
            }
        }
        return $compare;
    }
}